package edu.gatech.cs2340.thecyclonesquad;

import edu.gatech.cs2340.thecyclonesquad.database.Database;
import edu.gatech.cs2340.thecyclonesquad.models.InMemoryModel;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class FilterLostCategoryActivity extends Activity implements OnItemSelectedListener {
	
	Spinner spinner1;
	String[] categories = { "Category: Keepsakes", "Category: Heirlooms" , "Category: Misc" };
	String category;
	private ArrayAdapter<String> mListData7;


	/**
	 * Initializes our activity.
	 */
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filter_lost_category);
		// Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(FilterLostCategoryActivity.this, android.R.layout.simple_spinner_item, categories);
		spinner1 = (Spinner) findViewById(R.id.spin5);
		spinner1.setAdapter(adapter);
		spinner1.setOnItemSelectedListener(this);
	}

	/**
	 * Initialize the contents of the Activity's standard options menu.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_filter_lost_category, menu);
		return true;
	}

	/**
	 * Assigns values to category depending on spinner selection.
	 */
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		//gets position of one selected
		int position = spinner1.getSelectedItemPosition();
		switch(position) {
			case 0:
				category = "Keepsakes"; 
				break;
			case 1:
				category = "Heirlooms";
				break;
			case 2:
				category = "Misc";
				break;
		}
		
	}
	/**
	 * Method for if nothing selected in spinner.
	 */
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Filter lost items by category.
	 * @param view is the basic building block for UI components
	 */
	public void categoryFilter(View view) {
        //get handle of list
        ListView lvList7 = (ListView)findViewById(R.id.listView7);
        //creates an adapter to hold values for the list
        //Array adapter constructor has a List Item layout and the context
        //String items[] = lostitems.filterByCategory("Lost", category);
        //String items[] = founditems.filterByCategory(type, category);
        Database entry = new Database(this);
    	entry.open();
    	String items[] = entry.getItems("Lost", category);
    	entry.close();
        mListData7 = new ArrayAdapter<String>(FilterLostCategoryActivity.this, android.R.layout.simple_list_item_1, items);
        //Set adapter to the list
        lvList7.setAdapter(mListData7);
	}


	/**
	 * logOut method logs the user out and goes back to login page.
	 * @param view is the basic building block for UI components
	 */
	public void logOut(View view) {
		Intent intent = new Intent(this, SignInActivity.class);
		startActivity(intent);			
	}

}
